home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / BASE_QUE.C < prev    next >
C/C++ Source or Header  |  1992-09-29  |  6KB  |  167 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 08/29/89 -- Initial design and implementation
  13. // Updated: MBN 10/11/89 -- Changed "current_position" to "curpos" 
  14. // Updated: LGO 02/02/90 -- Re-wrote practically everything
  15. // Updated: MJF 03/12/90 -- Added group names to RAISE
  16. // Updated: VDN 02/21/92 -- New lite version
  17. // Updated: JAM 09/23/92 -- added def for growth_ratio_s
  18. //
  19. // The  CoolQueue class is publicly derived from the Generic class  and is used
  20. // to implement  non-type specific  functionality  for the  parameterized CoolQueue
  21. // class. In this  manner, code common to all  instances of the CoolQueue class can
  22. // be shared to reduce code replication. The CoolQueue class implements  a circular
  23. // buffer of  a   user-specified type.   This is accomplished   by   using  the
  24. // parameterized type capability  of C++. The CoolQueue  will grow  dynamically  as
  25. // necessary with the amount of growth determined by the value of an allocation
  26. // size slot. Fixed length  queues  are also supported by  setting the value of
  27. // the allocation size slot to INVALID.
  28. //
  29.  
  30. #ifndef BASE_QUEUEH                // If no class definition
  31. #include <cool/Base_Queue.h>            // Include definition file
  32. #endif
  33.  
  34. // growth_ratio_s -- If non-zero, growth ratio 
  35. float CoolBase_Queue::growth_ratio_s;
  36.  
  37.  
  38. // int length() -- Return the number of elements in this CoolBase_Queue
  39. // Input:           None
  40. // Output:          Integer representing number of elements
  41.  
  42. int CoolBase_Queue::length () const {
  43.   int len = in - out;
  44.   return (len < 0) ? limit + len : len;
  45. }
  46.  
  47. // void set_growth_ratio(float) -- Set growth percentage of this CoolBase_Queue
  48. // Input:                          Float ratio and character string type
  49. // Output:                         None
  50.  
  51. void CoolBase_Queue::set_growth_ratio (float ratio, const char* Type) {
  52. #if ERROR_CHECKING
  53.   if (ratio < 0.0) {                // If non-positive growth
  54.     //RAISE (Error, SYM(CoolQueue), SYM(Negative_Ratio),
  55.     printf ("CoolQueue<%s>::set_growth_ratio(): Negative growth ratio %f.\n",
  56.         Type, ratio);
  57.     abort ();
  58.   }
  59. #endif
  60.   this->growth_ratio_s = ratio;            // Set growth ratio
  61. }
  62.  
  63.  
  64. // void set_alloc_size(int) -- Set the default allocation size growth rate
  65. // Input:                      Integer size and character string type
  66. // Output:                     None
  67.  
  68. void CoolBase_Queue::set_alloc_size (int size, const char* Type) {
  69. #if ERROR_CHECKING
  70.   if (size < 0 && size != INVALID) {        // If index out of range
  71.     //RAISE (Error, SYM(CoolQueue), SYM(Negative_Size),
  72.     printf ("CoolQueue<%s>::set_alloc_size(): Negative growth size %d.\n",
  73.         Type, size);
  74.     abort ();
  75.   }
  76. #endif
  77.   this->alloc_size = size;            // Set growth size
  78. }
  79.  
  80.  
  81. // CoolBase_Queue () -- Empty constructor for the CoolBase_Queue class
  82. // Input:      None
  83. // Output:     None
  84.  
  85. CoolBase_Queue::CoolBase_Queue () {
  86.   this->limit = 0;                // Initialize size
  87.   this->in = this->out = 0;            // Intialize first/last pointer
  88.   this->alloc_size = QUEUE_MEM_BLK_SZ;        // Default
  89. }
  90.  
  91.  
  92. // CoolBase_Queue (int) -- Constructor that specifies number of elements
  93. // Input:          Integer number of elements
  94. // Output:         None
  95.  
  96. CoolBase_Queue::CoolBase_Queue (int n) {
  97.   this->limit = n;                // Element capacity
  98.   this->in = this->out = 0;            // Intialize first/last pointer
  99.   this->alloc_size = QUEUE_MEM_BLK_SZ;        // Default
  100. }
  101.  
  102.  
  103. // CoolBase_Queue (CoolBase_Queue&) -- Constructor for reference to another CoolBase_Queue
  104. // Input:            CoolBase_Queue reference
  105. // Output:           None
  106.  
  107. CoolBase_Queue::CoolBase_Queue (const CoolBase_Queue& s) {
  108.   alloc_size = s.alloc_size;            // Set alloc size
  109.   if (alloc_size == INVALID)
  110.     alloc_size = QUEUE_MEM_BLK_SZ;
  111.   this->in = s.in;                // Set in
  112.   this->out = s.out;                // Set out
  113.   this->limit = s.limit;            // Set limit
  114. }
  115.  
  116.  
  117. // ~CoolBase_Queue -- Destructor for CoolBase_Queue class that frees up storage
  118. // Input:    None
  119. // Output:   None
  120.  
  121. CoolBase_Queue::~CoolBase_Queue () {
  122. }
  123.  
  124.  
  125. // look_error -- Error message for parameterized CoolQueue<Type>::look() method
  126. // Input:        Character string of type
  127. // Output:       None
  128.  
  129. void CoolBase_Queue::look_error (const char* Type) {
  130.   //RAISE (Error, SYM(CoolQueue), SYM(No_Elements),
  131.   printf ("CoolQueue<%s>::look(): No elements in queue.\n", Type);
  132.   abort ();
  133. }
  134.  
  135.  
  136. // value_error -- Error message for parameterized CoolQueue<Type>::value() method
  137. // Input:         Character string of type
  138. // Output:        None
  139.  
  140. void CoolBase_Queue::value_error (const char* Type) {
  141.   //RAISE (Error, SYM(CoolQueue), SYM(Invalid_Cpos),
  142.   printf ("CoolQueue<%s>::value(): Invalid current position.\n", Type);
  143.   abort ();
  144. }
  145.  
  146.  
  147. // resize_error -- Error message for parameterized CoolQueue<Type>::resize() method
  148. // Input:          Character string of type
  149. // Output:         None
  150.  
  151. void CoolBase_Queue::resize_error (const char* Type) {
  152.   //RAISE (Error, SYM(CoolQueue), SYM(Static_Size),
  153.   printf ("CoolQueue<%s>::resize(): Static-size queue.\n", Type);
  154.   abort ();
  155. }
  156.  
  157.  
  158. // assign_error -- Error message for parameterized CoolQueue<Type>::operator=()
  159. // Input:          Character string of type
  160. // Output:         None
  161.  
  162. void CoolBase_Queue::assign_error (const char* Type) {
  163.   //RAISE (Error, SYM(CoolQueue), SYM(Static_Size),
  164.   printf ("CoolQueue<%s>::operator=(): Static-size queue.\n", Type);
  165.   abort ();
  166. }
  167.